from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

intent_options = {
    "weather_query": "查询天气",
    "restaurant_booking": "预订餐厅",
    "news_fetch": "获取新闻",
    "joke_telling": "讲个笑话"
}

llm = ChatOpenAI(model='gpt-4-turbo-preview',
				 api_key="sk-oYQsYEo1JPME6JEYo3AT9xBupdO3Nqb64sHUNZygh4txMLeC",
				 base_url="https://www.DMXapi.com/v1/")

# 定义意图识别的提示模板
template = """用户输入: {user_input}

请识别用户的意图，并输出意图选项，只输出选项，不输出其它字符: {intent_options}"""

prompt = ChatPromptTemplate.from_template(template)
output_parser = StrOutputParser()
# 创建LLM链
llm_chain = (prompt | llm | output_parser)

# 用户输入示例
user_input = "我想知道明天的天气"

# 识别意图
# 使用prompt | llm的方式识别意图
response = llm_chain.invoke({"user_input": user_input, "intent_options": intent_options})
print("识别的意图:", response)
